package com.two.service.user.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.Gson;
import com.two.common.entity.User;
import com.two.common.entity.UserAndPermissions;
import com.two.common.util.CommonResult;
import com.two.service.user.config.Msg;
import com.two.service.user.feign.CaipinOpenFeign;
import com.two.service.user.feign.RestaurantFeign;
import com.two.service.user.service.IUserService;
import com.two.service.user.vo.BohuiVo;
import com.two.service.user.vo.RestaurantVo;
import com.two.service.user.vo.UserVo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 汪瑞明
 * @since 2022-08-31
 */
@RestController
@RequestMapping("/user/user")
public class UserController {
    @Autowired
    private IUserService iUserService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private CaipinOpenFeign caipinOpenFeign;
    @Autowired
    private RestaurantFeign restaurantFeign;

//    //根据token查询用户
//    @PostMapping("/selectById")
//    public CommonResult selectById(HttpServletRequest request){
//        String token = request.getHeader("token");
//        System.out.println("token==========>"+token);
//
//        ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();
//        String s = opsForValue.get(token);
//        Map map = JSON.parseObject(s, Map.class);
//        return new CommonResult(2000,"根据id查询用户信息成功",map);
//    }


    //根据token查询用户
    @PostMapping("/selectById")
    public CommonResult selectById(String token){
        ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();
        String s = opsForValue.get(token);
        Map map = JSON.parseObject(s, Map.class);
        return new CommonResult(2000,"根据id查询用户信息成功",map);
    }
    //查询所有用户
    @PostMapping("/selectAllUser")
    public CommonResult selectAllUser(Integer currentPage, Integer pageSize, @RequestBody UserVo userVo){
       return iUserService.selectUser(currentPage,pageSize,userVo);
    }
    //修改用户信息
    @PostMapping("/edit")
    public CommonResult edit(@RequestBody User user){
        iUserService.updateById(user);
        return new CommonResult(2000,"修改成功",null);
    }
    //删除用户
    @PostMapping("del")
    public CommonResult del(Integer id){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("is_deleted","0");
        queryWrapper.like("userid",id);
        iUserService.remove(queryWrapper);
        return new CommonResult(2000,"删除成功",null);
    }

    //发送验证码
    @PostMapping("sendMsg")
    public CommonResult send(@RequestBody User user){
        String phone= user.getPhone();
        String random = (Math.random()+"").substring(2,8);
        Msg.sendMeg(phone,random);
        return new CommonResult(2000,"发送成功",random);
    }
    //添加注册用户
    @PostMapping("zhuce")
    public CommonResult zhuce(@RequestBody User user){
        Md5Hash md5Hash=new Md5Hash(user.getPassword(),"abc",1024);
        user.setPassword(""+md5Hash);
        System.out.println("头像"+user.getUrl());
        user.setSalt("abc");
        user.setIsDeleted(0);

      return  iUserService.insert(user);
    }
    //查询申请菜品
    @PostMapping("selectCook")
    public CommonResult selectCook(@RequestParam Integer currentPage, @RequestParam Integer pageSize,@RequestParam String token, @RequestBody RestaurantVo restaurantVo){
        return caipinOpenFeign.shen(currentPage,pageSize,token,restaurantVo);
    }

    //审核通过
    @PostMapping("pass")
    public CommonResult pass(@RequestParam String token,@RequestParam String id){
        System.out.println("审核通过");
     return caipinOpenFeign.pass(token,id);
    }

    //审核驳回
    @PostMapping("bohui")
    public CommonResult bohui(@RequestBody BohuiVo bohuiVo, @RequestParam String mid, @RequestParam String token){
       return caipinOpenFeign.bohui(bohuiVo,mid,token);
    }

    //查询全部饭店信息
    @PostMapping("selectAll")
    public CommonResult selectAll(){
        return restaurantFeign.selectAll();
    }

    //手机端用户注册
    @PostMapping("cellzhuce")
    public CommonResult cellzhuce(@RequestBody User user){
        Md5Hash md5Hash=new Md5Hash(user.getPassword(),user.getSalt(),1024);
        user.setPassword(""+md5Hash);
        user.setIsDeleted(0);
        user.setRoleid(3);
        user.setPhone(user.getUsername());
        user.setUsermonicker(user.getUsername());
        user.setRolename("用户");
        user.setUrl("hangzhou.aliyuncs.com/2022/6/10/7ba1b785614741c8b153729c26d4fa9c9.jpg");
       return iUserService.insert(user);
    }

    //手机端查询当前用户角色Id
    @PostMapping("insertRoleId")
    public CommonResult pass( String token) {
        ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();
        String s = opsForValue.get(token);
        Gson gson = new Gson();
        UserAndPermissions userAndPermissions = gson.fromJson(s, UserAndPermissions.class);
        User user = userAndPermissions.getUser();
        Integer roleid = user.getRoleid();
        return new CommonResult(2000,"查询成功",roleid);
    }


}
